package service;

import dao.ProductDao;
import donain.PageBean;
import donain.Product;
import exception.ProductException;
import utilts.ResponseCode;

import java.sql.SQLException;
import java.util.List;

public class ProductService {
    private ProductDao dao = new ProductDao();
    // 添加商品
    public boolean addProduct(Product p) throws ProductException {
        try {

          dao.addProduct(p);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProductException(ResponseCode.EEROR_ADD_GOODS);
        }
    }
    // 查找所有商品信息
    public List<Product> listAll() throws ProductException {
        try {
            return dao.listAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProductException(ResponseCode.EEROR_QUERY_GOODS);
        }
    }
    // 分页操作
    public PageBean findProductByPage(int currentPage, int currentCount,
                                      String category) {
        PageBean bean = new PageBean();
        // 封装每页显示数据条数
        bean.setCurrentCount(currentCount);
        // 封装当前页码
        bean.setCurrentPage(currentPage);
        // 封装当前查找类别
        bean.setCategory(category);
        try {
            // 获取总条数
            int totalCount = dao.findAllCount(category);
            bean.setTotalCount(totalCount);
            // 获取总页数
            int totalPage = (int) Math.ceil(totalCount * 1.0 / currentCount);
            bean.setTotalPage(totalPage);
            // 获取当前页数据
            List<Product> ps = dao.findByPage(currentPage, currentCount,
                    category);
            bean.setPs(ps);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bean;
    }
    // 根据id查找商品
    public Product findProductById(String id) throws ProductException {
        try {
            return dao.findProductById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProductException(ResponseCode.EEROR_QUERY_ID_GOODS);
        }
    }
    // 下载销售榜单
    public List<Object[]> download(String year, String month) {
        List<Object[]> salesList = null;
        try {
            salesList = dao.salesList(year, month);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return salesList;
    }
    // 多条件查询
    public List<Product> findProductByManyCondition(String id, String name,
                                                    String category, String minprice, String maxprice) {
        List<Product> ps = null;
        try {
            ps = dao.findProductByManyCondition(id, name, category, minprice,
                    maxprice);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }
    // 修改商品信息
    public boolean editProduct(Product p) throws ProductException {
        try {
            dao.editProduct(p);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProductException(ResponseCode. EEROR_EDI_PRODUCT);
        }
    }
    //前台，获取本周热销商品
    public List<Object[]> getWeekHotProduct() throws ProductException {
        try {
            return dao.getWeekHotProduct();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ProductException(ResponseCode.EEROR_QUERY_GOODS);
            }
    }
    //前台，用于搜索框根据书名来模糊查询相应的图书
    public PageBean findBookByName(int currentPage, int currentCount,
                                   String searchfield) {
        PageBean bean = new PageBean();
        // 封装每页显示数据条数
        bean.setCurrentCount(currentCount);
        // 封装当前页码
        bean.setCurrentPage(currentPage);
        // 封装模糊查询的图书名
        bean.setSearchfield(searchfield);
        try {
            // 获取总条数
            int totalCount = dao.findBookByNameAllCount(searchfield);
            bean.setTotalCount(totalCount);

            // 获取总页数
            int totalPage = (int) Math.ceil(totalCount * 1.0 / currentCount);
            bean.setTotalPage(totalPage);

            //满足条件的图书
            List<Product> ps = dao.findBookByName(currentPage,currentCount,searchfield);
            bean.setPs(ps);
            return bean;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("前台搜索框根据书名查询图书失败！");
        }
    }
    //后台系统，根据id删除商品信息
    public boolean deleteProduct(String id) throws ProductException {
        try {
            dao.deleteProduct(id);
            return true;
        } catch (SQLException e) {
            throw new ProductException(ResponseCode.EEROR_DET_PRODUCT);
        }
    }
    public  List<Object> getCategory (int num) throws  ProductException {
        try {
            return  dao.getCategory(num);
        }catch (SQLException e){
            throw new  ProductException(ResponseCode.EEROR_QUERY_CATEGORU);
        }
    }
    public  List<Product> findPraductByCategory(String categcry) throws  ProductException{
        try {
            return  dao.findPraductByCategory(categcry);
        }catch (SQLException e){
            throw  new ProductException(ResponseCode.EEROR_QUERY_CATEGORU);
        }
    }
    public  List<Product> findBookName(String name) throws ProductException {
        try {
            return  dao.findBookName(name);
        }catch (SQLException e){
            throw  new ProductException(ResponseCode.ERROR_QUERY_PRODUCT_NAME);
        }

    }
}
